package com.baifendian.common.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import org.springframework.util.ObjectUtils;

/**
 * @author peng.guo on 2018/3/6.
 */
public class DateUtils {

  private static String PATTERN = "MM/dd/yyyy";

  public static Date stringToDate(String str,String pattern){
//    String str = "01/01/2015";
    if(ObjectUtils.isEmpty(pattern)){
      pattern = PATTERN;
    }
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
    LocalDate dateTime = LocalDate.parse(str, formatter);
//    System.out.println(dateTime.format(formatter));// not using toString

    ZoneId zoneId = ZoneId.systemDefault();
    ZonedDateTime zdt = dateTime.atStartOfDay(zoneId);
    Date date = Date.from(zdt.toInstant());

    return date;
  }

  public static Date stringToDate(String str){
    return stringToDate(str,null);
  }

  public static LocalDateTime getCurrentTime(){
    return LocalDateTime.now();
  }

  public static Date localDateTimeToDate(LocalDateTime localDateTime){
    ZoneId zone = ZoneId.systemDefault();
    Instant instant = localDateTime.atZone(zone).toInstant();
    java.util.Date date = Date.from(instant);
    return date;
  }

}
